import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DecimalFormat;

/**
 * Created by Administrator on 2017/5/5.
 */
public class EMDClient {
    public static void main(String[] args) {
        try {
            String PROTOCOL = "C200";
            String TEMPCODE = "5601";
            String TRANSBRANCH = "9909";
            String SRCSYSTEM = "56";
            String ADDRESS = "18650495842";
            String TIMESTAMP = "20161206094711";
            String MSG = "这是你的内容";
            String _EmdUtil = "com.csii.great.end.data.EmdUtil@dfkdsjfs";
            String MSGCOUNT= "1";
            String RESULTINFO ="交易成功";
            StringBuffer sbClient = new StringBuffer();

            sbClient.append("<?xml version=\"1.0\" encoding=\"gb2312\" ?>");
            sbClient.append("<EMD>");
            sbClient.append("<HEAD>");
            sbClient.append("<PROTOCOL>");
            sbClient.append(PROTOCOL);
            sbClient.append("</PROTOCOL>");
            sbClient.append("<TEMPCODE>");
            sbClient.append(TEMPCODE);
            sbClient.append("</TEMPCODE>");
            sbClient.append("<TRANSID></TRANSID>");
            sbClient.append("<TRANSCOMMAND></TRANSCOMMAND>");
            sbClient.append("<TRANSBRANCH>");
            sbClient.append(TRANSBRANCH);
            sbClient.append("</TRANSBRANCH>");
            sbClient.append("<SRCSYSTEM>");
            sbClient.append(SRCSYSTEM);
            sbClient.append("</SRCSYSTEM>");
            sbClient.append("<SRCDEVICE></SRCDEVICE>");
            sbClient.append("<TIMESTAMP>");
            sbClient.append(TIMESTAMP);
            sbClient.append("</TIMESTAMP>");
            sbClient.append("</HEAD>");
            sbClient.append("<BODY>");
            sbClient.append("<ADDRESS>");
            sbClient.append(ADDRESS);
            sbClient.append("</ADDRESS>");
            sbClient.append("<MSG></MSG>");
            sbClient.append("</BODY>");
            sbClient.append("</EMD>");
            int len = sbClient.toString().getBytes().length;
            String lenStr=len+"";
            DecimalFormat df=new DecimalFormat("00000");
            lenStr=df.format(Integer.parseInt(lenStr));
            sbClient.insert(0,lenStr);
            //1.建立客户端socket连接，指定服务器位置及端口
            Socket socket =new Socket("localhost",8800);
            //2.得到socket读写流
            OutputStream os=socket.getOutputStream();
            PrintWriter pw=new PrintWriter(os);
            //输入流
            InputStream is=socket.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            //3.利用流按照一定的操作，对socket进行读写操作
            pw.write(sbClient.toString());
            pw.flush();
            socket.shutdownOutput();
            //接收服务器的相应
            String sbServer=null;
            while(!((sbServer=br.readLine())==null)){
                System.out.println("接收服务器的信息："+sbServer);
            }
            //4.关闭资源
            br.close();
            is.close();
            pw.close();
            os.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
